// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Plugin.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework.LiveObjects
{
    #region Usings

    using System;

    using Org.OpenTrader.Framework.Enums;
    using Org.OpenTrader.Framework.Events;
    using Org.OpenTrader.Framework.LiveObjects.Interfaces;

    #endregion

    /// <summary>
    /// The plugin.
    /// </summary>
    public abstract class Plugin : LiveObject, IPlugin
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="Plugin"/> class.
        /// </summary>
        /// <param name="descriptor">
        /// The descriptor.
        /// </param>
        /// <param name="container">
        /// The container.
        /// </param>
        public Plugin(ILiveObjectDescriptor descriptor, ILiveObjectContainer container)
            : base(descriptor, container)
        {
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// The activate.
        /// </summary>
        /// <param name="service">
        /// The service.
        /// </param>
        /// <param name="plugin">
        /// The plugin.
        /// </param>
        public void Activate(IService service, IPlugin plugin)
        {
            // Change Current Directory for this domain
            Environment.CurrentDirectory = plugin.Container.Descriptor.WorkingDirectory;

            this.LiveObjectsEventCB.RaiseEvent(this, new LiveObjectsEventArgs("Activating " + plugin.Descriptor.Name));

            // Post activation event to plugin
            plugin.OnActivation(
                AppDomain.CurrentDomain, new ActivationEventArgs.CreateDestroy(EActivationType.Activation));

            // Post plugin activation event to owner service
            service.OnActivation(
                AppDomain.CurrentDomain, 
                new ActivationEventArgs.OwnedPluginCreateDestroy(EActivationType.Activation, plugin));

            this.LiveObjectsEventCB.RaiseEvent(this, new LiveObjectsEventArgs("Activated " + plugin.Descriptor.Name));
        }

        /// <summary>
        /// The desactivate.
        /// </summary>
        /// <param name="service">
        /// The service.
        /// </param>
        /// <param name="plugin">
        /// The plugin.
        /// </param>
        public void Desactivate(IService service, IPlugin plugin)
        {
            // Change Current Directory for this domain
            Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory;

            this.LiveObjectsEventCB.RaiseEvent(
                this, new LiveObjectsEventArgs("Desactivating " + plugin.Descriptor.Name));

            // Post activation event to plugin
            plugin.OnActivation(
                AppDomain.CurrentDomain, new ActivationEventArgs.CreateDestroy(EActivationType.Desactivation));

            // Post plugin activation event to owner service
            service.OnActivation(
                AppDomain.CurrentDomain, 
                new ActivationEventArgs.OwnedPluginCreateDestroy(EActivationType.Desactivation, plugin));

            this.LiveObjectsEventCB.RaiseEvent(this, new LiveObjectsEventArgs("Desctivated " + plugin.Descriptor.Name));
        }

        #endregion
    }
}